// 玩家ID持久化管理

class PlayerIdManager {
    // 获取持久化的玩家ID
    static getPlayerId() {
        // 尝试从localStorage获取已存储的ID
        let playerId = localStorage.getItem('tankOnlinePlayerId');
        
        // 如果没有存储的ID，则返回null
        return playerId;
    }
    
    // 保存玩家ID到localStorage
    static savePlayerId(playerId) {
        if (playerId) {
            localStorage.setItem('tankOnlinePlayerId', playerId);
        }
    }
    
    // 生成并保存新的玩家ID
    static generatePlayerId() {
        // 生成一个随机ID
        const randomId = 'player_' + Math.random().toString(36).substr(2, 9);
        this.savePlayerId(randomId);
        return randomId;
    }
    
    // 获取或创建玩家ID
    static getOrCreatePlayerId() {
        let playerId = this.getPlayerId();
        if (!playerId) {
            playerId = this.generatePlayerId();
        }
        return playerId;
    }
    
    // 清除存储的玩家ID
    static clearPlayerId() {
        localStorage.removeItem('tankOnlinePlayerId');
    }
}